package edu.ccut.titan.tag.chart;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;

import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspWriter;

import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.imagemap.ImageMapUtilities;

import edu.ccut.titan.tag.chart.utils.ChartInfo;
import edu.ccut.titan.tag.chart.utils.ServletChartDeleter;
import edu.ccut.titan.tag.complex.AbstractComplexTag;

public abstract class TitanAbstractChartTag extends AbstractComplexTag {
	private static final String CHART_TMP_DIRETORY = "charttmp";
	
	public TitanAbstractChartTag(String tagKey) {
		super(tagKey);
	}

	protected void writeChart(ChartInfo chartInfo) {
		ServletChartDeleter servletChartDeleter = getServerChartDeleter();
		ChartRenderingInfo chartRenderingInfo = new ChartRenderingInfo(
				new StandardEntityCollection());
		
		String chartFileName = getChartFileName();
		String realChartFileName = getRealChartFilePath() + chartFileName + ".png";;
		String relativeChartFileName = getRelativeChartFilePath() + chartFileName + ".png";;

		createChartFile(chartRenderingInfo, chartInfo, getRealChartFilePath(), chartFileName + ".png");
		servletChartDeleter.addChart(realChartFileName);
		JspWriter out = super.getWriter();
		PrintWriter writer = null;
		try {
			writer = new PrintWriter(out);
			ImageMapUtilities.writeImageMap(writer, chartFileName,
					chartRenderingInfo);

			out.print("<div align=\"center\"><img border=\"0\" src=\"");
			out.print(relativeChartFileName);
			out.print("\" width=\"");
			out.print(String.valueOf(chartInfo.getWidth()).trim());
			out.print("\" height=\"");
			out.print(String.valueOf(chartInfo.getHeight()).trim());
			out.print("\" usemap=\"#");
			out.print(chartFileName);
			out.print("\" alt=\"");
			out.print(relativeChartFileName);
			out.println("\"/></div>");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private String getChartFileName() {
		Calendar calendar = Calendar.getInstance();
		String chartFileName = "chart"
				+ String.valueOf(calendar.getTimeInMillis());
		return chartFileName;
	}

	private ServletChartDeleter getServerChartDeleter() {
		HttpSession session = super.pageContext.getSession();

		ServletChartDeleter servletChartDeleter = (ServletChartDeleter) session
				.getAttribute("Chart_Deleter");

		if (servletChartDeleter == null) {
			servletChartDeleter = new ServletChartDeleter();
			session.setAttribute("Chart_Deleter", servletChartDeleter);
		}
		
		return servletChartDeleter;
	}

	private String getRelativeChartFilePath() {
		String relativeChartFilePath = super.getRequest().getContextPath()
				+ "/" + CHART_TMP_DIRETORY + "/";
		return relativeChartFilePath;
	}

	private String getRealChartFilePath() {
		String realChartFilePath = super.pageContext.getServletContext().getRealPath("")
				+ File.separator + CHART_TMP_DIRETORY + File.separator;
		
		return realChartFilePath;
	}

	
	private void createChartFile(ChartRenderingInfo chartRenderingInfo,
			ChartInfo chartInfo, String realChartFilePath, String fileName) {

		File chartDir = new File(realChartFilePath);
		if (!chartDir.exists()) {
			chartDir.mkdir();
		}
		
		File chartFile = new File(realChartFilePath + fileName);
		try {
			ChartUtilities.saveChartAsPNG(chartFile, chartInfo.getChart(),
					chartInfo.getWidth(), chartInfo.getHeight(),
					chartRenderingInfo);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}